package com.andculture.puremvcflexunittesting.example.model {
	import com.andculture.puremvcflexunittesting.PureMVCNotificationEvent;
	import com.andculture.puremvcflexunittesting.PureMVCTestCase;
	import com.andculture.puremvcflexunittesting.example.ApplicationFacade;
	
	import flexunit.framework.TestSuite;
	
	import org.puremvc.as3.core.View;
	import org.puremvc.as3.interfaces.IView;
	
	/**
	 * Test the UserProxy.
	 */
	public class UserProxyTest extends PureMVCTestCase {
		
		/****************************************
		 * GETTERS AND SETTERS
		****************************************/
		
		/**
		 * Gets the UsersProxy instance to run tests against.
		 */
		private function get proxy():UserProxy {
			return ApplicationFacade.getInstance().retrieveProxy(UserProxy.NAME) as UserProxy;
		}
		
		/**
		 * Gets the UserModule view to register observers.
		 */
		private function get view():IView {
			return View.getInstance();
		}
		
		/**
		 * Constructor.
		 * 
		 * @param methodName Optional test method to run.
		 */
		public function UserProxyTest(methodName:String = null) {
			super(methodName);
		}
		
		/****************************************
		 * PUBLIC METHODS
		****************************************/
		
		/**
		 * Creates and returns the TestSuite for this TestCase.
		 * 
		 * @returns TestSuite describing this TestCase.
		 */
		public static function suite():TestSuite {
			var suite:TestSuite = new TestSuite();
			suite.addTestSuite(UserProxyTest);
			return suite;
		}
		
		/****************************************
		 * TEST METHODS
		****************************************/
		
		/**
		 * Test listing all users.
		 */
		public function testListUsers():void {
			registerObserver(this.view, this.proxy, UserProxy.LIST_USERS_RESPONSE, handleListUsersResponse, 1000);
			this.proxy.listUsers();
		}
		
		/**
		 * Simulates an asynchronous test that fails.
		 */
		public function testFailure():void {
			registerObserver(this.view, this.proxy, UserProxy.LIST_USERS_RESPONSE, handleListUsersResponseFail, 1000);
			this.proxy.listUsers();
		}
		
		/****************************************
		 * NOTIFICATION HANDLERS
		****************************************/
		
		/**
		 * Handler method called when listUsers() is complete.
		 * 
		 * @param e PureMVCNotificationEvent fired which contains the
		 * sent Notification.
		 */
		private function handleListUsersResponse(e:PureMVCNotificationEvent):void {
			// Pull the array of users from the notification.
			var users:Array = e.notification.getBody() as Array;
			
			// Run a few very simple tests.
			assertNotNull("Users returned is null", users);
			assertTrue("No users returned.", users.length > 0);
			assertEquals(users.length, 3);
		}
		
		/**
		 * Handler method called when listUsers() is complete. This test
		 * is meant to always fail.
		 * 
		 * @param e PureMVCNotificationEvent fired which contains the
		 * sent Notification.
		 */
		private function handleListUsersResponseFail(e:PureMVCNotificationEvent):void {
			// Pull the array of users from the notification.
			var users:Array = e.notification.getBody() as Array;
			
			// Users is not null so this will fail.
			assertNull(users);
		}
	}
}